package com.syxp.pda.action.detail;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.bean.FieldBean;
import com.syxp.pda.core.util.Operation;
import com.syxp.pda.core.util.TimeFormatUtil;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.detail.CheckPlanDetail;
import com.syxp.pda.entity.detail.CheckPlanDetailDTO;
import com.syxp.pda.entity.detail.CheckPlanDetailUtil;
import com.syxp.pda.entity.detail.ExamCountBean;
import com.syxp.pda.service.detail.ExamManagerService;

public class ExamManagerAction extends BaseAction{

	@Resource
	private ExamManagerService examManagerService ;
	
	public ExamManagerService getExamManagerService() {
		return examManagerService;
	}


	public void setExamManagerService(ExamManagerService examManagerService) {
		this.examManagerService = examManagerService;
	}

	private CheckPlanDetailDTO planDetail ;
	
	public CheckPlanDetailDTO getPlanDetail() {
		return planDetail;
	}


	public void setPlanDetail(CheckPlanDetailDTO planDetail) {
		this.planDetail = planDetail;
	}

	public String examCount() {
		try{
			List<ExamCountBean> planDetails = examManagerService.examCount(buildFieldBean(planDetail)) ;
			JsonObject jsonObj = new JsonObject() ;
			jsonObj.add("exams", gson.toJsonTree(planDetails)) ;
			jsonObj.addProperty("success", true) ;
			this.jsonString=gson.toJson(jsonObj) ;
			System.out.println(this.jsonString) ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String placeCount() {
		try{
			List<Object[]> planDetails = examManagerService.placeCount(buildFieldBean(planDetail)) ;
			JsonArray tree = new JsonArray() ;
			int arrive = 0 ;
			int unArrive = 0 ;
			for(Object[] objs : planDetails){
				JsonObject jobj = new JsonObject() ;
				jobj.addProperty("planName", String.valueOf(objs[0])) ;
				jobj.addProperty("lineName", String.valueOf(objs[1])) ;
				jobj.addProperty("zoneName", String.valueOf(objs[2])) ;
				jobj.addProperty("dutyName", String.valueOf(objs[3])) ;
				jobj.addProperty("claName", String.valueOf(objs[4])) ;
				jobj.addProperty("turnOrder", String.valueOf(objs[5])) ;
				jobj.addProperty("startTime", TimeFormatUtil.formatDate((Date)objs[6])) ;
				jobj.addProperty("endTime", TimeFormatUtil.formatDate((Date)objs[7])) ;
				jobj.addProperty("userName", String.valueOf(objs[9])) ;
				jobj.addProperty("operatorCode", String.valueOf(objs[10])) ;
				if(((Integer)objs[8]) != 0){
					jobj.addProperty("isArrive", "是") ;
					arrive++ ;
				}else{
					jobj.addProperty("isArrive", "否") ;
					unArrive++ ;
				}
				tree.add(jobj) ;
			}
			JsonObject jsonObj = new JsonObject() ;
			jsonObj.add("places", tree) ;
			jsonObj.addProperty("arrive", arrive) ;
			jsonObj.addProperty("unArrive", unArrive) ;
			jsonObj.addProperty("success", true) ;
			this.jsonString=gson.toJson(jsonObj) ;
			System.out.println(this.jsonString) ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String workCount() {
		try{
			List<CheckPlanDetailUtil> planDetails = examManagerService.workCount(buildFieldBean(planDetail)) ;
			JsonObject jsonObj = new JsonObject() ;
			jsonObj.add("workTime", gson.toJsonTree(planDetails)) ;
			jsonObj.addProperty("success", true) ;
			this.jsonString=gson.toJson(jsonObj) ;
			System.out.println(this.jsonString) ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}

	public String unCheckCount() {
		try{
			List<CheckPlanDetail> planDetails = examManagerService.unCheckList(buildFieldBean(planDetail)) ;
			List<Object[]> list = examManagerService.unCheckCount(buildFieldBean(planDetail)) ;
			JsonArray jarray = new JsonArray() ;
			for(Object[] objs : list) {
				JsonObject obj = new JsonObject() ;
				obj.addProperty("lineName", String.valueOf(objs[1])) ;
				obj.addProperty("count", String.valueOf(objs[0])) ;
				jarray.add(obj) ;
			}
			JsonObject jsonObj = new JsonObject() ;
			jsonObj.add("detail", gson.toJsonTree(planDetails)) ;
			jsonObj.add("unCheck", jarray) ;
			jsonObj.addProperty("success", true) ;
			this.jsonString=gson.toJson(jsonObj) ;
			System.out.println(this.jsonString) ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	private List<FieldBean> buildFieldBean(CheckPlanDetailDTO o) throws IllegalArgumentException, IllegalAccessException, ParseException {
		Field[] fields = o.getClass().getDeclaredFields() ;
		Field.setAccessible(fields, true) ;
		List<FieldBean> qf = new ArrayList<FieldBean>() ;
		System.out.println(o) ; 
		for(Field field : fields) {
				if(field.getType().getName().equals("java.lang.String")&&field.get(o)!=null&&!((String)field.get(o)).equals("")){
					if(field.getName().equals("startTime")){
						qf.add(new FieldBean("start_PDA_CHKDateTime" ,TimeFormatUtil.parseDateF((String)field.get(o)),Operation.GE)) ;
					}else if(field.getName().equals("endTime")) {
						qf.add(new FieldBean("end_PDA_CHKDateTime" ,TimeFormatUtil.parseDateF((String)field.get(o)),Operation.LE)) ;
					}else{
						qf.add(new FieldBean(field.getName() ,field.get(o),Operation.LIKE)) ;
					}
				}else if(field.getType().getName().equals("int")&&field.getInt(o)!=0){
					qf.add(new FieldBean(field.getName() ,field.get(o),Operation.EQUAL)) ;
				}
		}
		return  qf;
	}
}
